//
//  TFColorSelection.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/6.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFColorSelection: UIView {
    @objc public var scrawlSelection:Bool = false
    @objc public var colorSelection:Bool = false
    @objc public var WidthSelection:Bool = false
    func maskPath()
    {
        let maskPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners:[UIRectCorner.topLeft,UIRectCorner.bottomLeft], cornerRadii: CGSize(width: 4, height: 4))
       
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }

    @objc public func colorSelectionAddTarget(_ target:NSObject,action:Selector)

    {
        if (self.colorSelection==false) {
            self.colorSelection = true
            self.maskPath()
            for i in 0..<5 {
          
                let selection  = TFSelectionButton()
                selection.backgroundColor = UIColor.clear
                let selectionW:CGFloat = self.frame.size.height
                let selectionH:CGFloat = selectionW
                    selection.tag = i
                let selectionY:CGFloat =   0
                let selectionX:CGFloat = CGFloat(i)*self.frame.size.height

                selection.frame = CGRect(x: selectionX, y: selectionY, width: selectionW, height: selectionH)
                self.addSubview(selection)
                selection.addTarget(target, action: action, for: .touchUpInside)
                      
                        
                let color  = UIView()
          
                        
                        if(i==0){
                            
                        color.backgroundColor = UIColor(red: 248.0/255.0, green: 70.0/255.0, blue: 70.0/255.0, alpha: 1)
                        }else if(i==1){
                            
                        color.backgroundColor = UIColor(red: 255.0/255.0, green: 206.0/255.0, blue: 45.0/255.0, alpha: 1)
                        }else if(i==2){
                            
                         color.backgroundColor = UIColor(red: 70.0/255.0, green: 147.0/255.0, blue: 248.0/255.0, alpha: 1)
                        }else if(i==3){
                            
                        color.backgroundColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1)
                        }else if(i==4){
                            
                        color.backgroundColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1)
                        }
                let colorW:CGFloat = self.frame.size.height/2;
                let colorH:CGFloat =  colorW
                        color.isUserInteractionEnabled = false
                let colorY:CGFloat =   colorH - (colorH/2.0);
                let colorX:CGFloat = CGFloat(i)*self.frame.size.height+(colorW/2);
                    color.frame = CGRect(x: colorX, y: colorY, width: colorW, height: colorH)
              
                selection.selectionColor = color.backgroundColor!
                self.addSubview(color)
                        
                        
                     
                    }
        }
      
    }

    @objc public func scrawlSelectionAddTarget(_ target:NSObject,action:Selector)
    {
        
        if (self.scrawlSelection==false) {
            self.scrawlSelection = true
            self.maskPath()
            for i in 0..<5 {
                       
                let selection  = TFSelectionButton()
                selection.backgroundColor = UIColor.clear
                let selectionW:CGFloat = self.frame.size.height
                let selectionH:CGFloat = selectionW
                    selection.tag = i
                let selectionY:CGFloat =   0
                let selectionX:CGFloat = CGFloat(i)*self.frame.size.height

                selection.frame = CGRect(x: selectionX, y: selectionY, width: selectionW, height: selectionH)
                self.addSubview(selection)
                selection.addTarget(target, action: action, for: .touchUpInside)
                    
                let button  = UIImageView()
            
                       
                       if(i==0){
                           
                          button.image = UIImage.init(named: "HTLive.bundle/S_1vN")
               
                       }else if(i==1){
                           
                       button.image = UIImage.init(named: "HTLive.bundle/line_1vN")
                       }else if(i==2){
                           
                       button.image = UIImage.init(named: "HTLive.bundle/dottedLine_1vN")
                       }else if(i==3){
                          
                     button.image =  UIImage.init(named: "HTLive.bundle/rectangular_1vN")
                       }else if(i==4){
                          
                    button.image =  UIImage.init(named: "HTLive.bundle/round_1vN")
                       }
                let colorW:CGFloat = 20
                let colorH:CGFloat =  colorW;
                      
                let colorY:CGFloat =  (self.frame.size.height - colorH)/2.0
                let colorX:CGFloat = CGFloat(i)*self.frame.size.height+(colorW/2);
                button.frame = CGRect(x: colorX, y: colorY, width: colorW, height: colorH)
             

                selection.selectionImage = button.image ?? UIImage()
                self.addSubview(button)
                       
                       
                    
                   }
        }
     
        
    }
    
    
    @objc public func WidthSelectionAddTarget(_ target:NSObject,action:Selector)
    {
        
        if (self.WidthSelection==false) {
            self.WidthSelection = true
            self.maskPath()
            for i in 0..<4 {
                   
                let selection  = TFSelectionButton()
                selection.backgroundColor = UIColor.clear
                let selectionW:CGFloat = self.frame.size.height
                let selectionH:CGFloat = selectionW
                    selection.tag = i
                let selectionY:CGFloat =   0
                let selectionX:CGFloat = CGFloat(i)*self.frame.size.height

                selection.frame = CGRect(x: selectionX, y: selectionY, width: selectionW, height: selectionH)
                self.addSubview(selection)
                selection.addTarget(target, action: action, for: .touchUpInside)
       
                let color  = UIView()
                   color.isUserInteractionEnabled = false
                   color.backgroundColor = UIColor.white
                var colorW:CGFloat = 0
                   
                let widthSuper:CGFloat = self.frame.size.height/4
                   
                    if (i == 0) {
                       colorW = widthSuper*(1.0/4.0)
                   }
                 else  if (i == 1) {
                       colorW = widthSuper*(2.0/4.0)
                   }
                 else  if (i == 2) {
                       colorW = widthSuper*(3.0/4.0)
                   }
                 else  if (i == 3) {
                     colorW =   widthSuper
                   }
                   
                let colorH:CGFloat =  colorW
              
                let colorY:CGFloat = (self.frame.size.height-colorH)/2.0;
                let colorX:CGFloat = CGFloat(i)*self.frame.size.height+(widthSuper*1.5);
                   
                color.frame = CGRect(x: colorX, y: colorY, width: colorW, height: colorW)
               
                color.layer.cornerRadius = colorW/2;
                   
                   // 设置裁剪
                color.layer.masksToBounds = true
                   
                selection.selectionWidth = colorW
                self.addSubview(color)
                 
                   
               }
        }
       
    }
}
